package  
{
	import flash.display.Sprite;
	import flash.display.StageDisplayState;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	import shapes.shapes2D.Circle;
	import shapes.shapes2D.distribution.goldenRatio.GoldenRatio;
	import shapes.shapes2D.Quad;
	
	/**
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 */
	public class PRS extends Sprite 
	{
		
		private var tf:TextFormat;
		private var txt:TextField;
		private var interval:int;
		
		private var quad:Quad;
		private var circle:Circle;
		private var matrix:Matrix;
		private var angle:Number = 0;
		private var sampleId:int;
		
		public function PRS() 
		{
			if ( _stage == null )
			{
				_stage = stage;
				_stage.displayState = StageDisplayState.FULL_SCREEN;
			}
			_stage.addEventListener( MouseEvent.MOUSE_DOWN, mouseHandler );
			quad = new Quad( new Rectangle( 0, 0, 150 * GoldenRatio.RATIO, 150 ) );
			quad.draw();
			addChild( quad );
			
			circle = new Circle( 0,0, 100 );
			circle.draw();
			addChild( circle );
			
			matrix = new Matrix( 1, 0, 0, 1, _stage.stageWidth / 2, _stage.stageHeight / 2 );
			
			circle.visible = true;
			quad.visible = false;
			quad.transform.matrix = matrix;
			circle.transform.matrix = matrix;
			
			txt = new TextField();
			txt.autoSize = TextFieldAutoSize.LEFT;
			txt.antiAliasType = AntiAliasType.ADVANCED;
			addChild( txt );
			txt.x = 200;
			tf = new TextFormat( 'Verdana', 16, 0xFFFFFF );
			
			//interval = setInterval( update, 20, 0 );
			
		}
		
		private function mouseHandler(e:MouseEvent):void 
		{
			
			
			clearInterval( interval );
			matrix = new Matrix( 1, 0, 0, 1, _stage.stageWidth / 2, _stage.stageHeight / 2 );
			interval = setInterval( update, 10, ( sampleId++ ) % 4 );
			
			
		}
		
		private function update( operation:int = 0 ):void 
		{
			circle.visible = true;
			quad.visible = false;
			switch( operation )
			{
				case 0:
					translate( Math.cos( angle ) * 4, 0 );
				break;
				
				case 1:
					circle.visible = false;
					quad.visible = true;
					rotate( Math.PI / 180 );
				break;
				
				case 2:
					scale( Math.cos( angle * 1.5 ) * 2, Math.sin( angle ) * 2 );
				break;
				
				case 3:
					skew( Math.cos( angle ), Math.sin( angle ) );
				break;
			}
			txt.setTextFormat( tf );
			angle += Math.PI / 180;
			
		}
		
		
		private function translate( tx:Number = 0, ty:Number = 0 ):void 
		{
			
			matrix.tx += tx;
			matrix.ty += ty;
			txt.text = 'translation\nmatrix.tx = tx;\nmatrix.ty = ty;';
			circle.transform.matrix = matrix;
			
		}
		
		private function rotate( angle:Number = 0 ):void 
		{
			
			var sin:Number = Math.sin( angle );
			var cos:Number = Math.cos( angle );
			
			var a:Number = matrix.a;
			var b:Number = matrix.b;
			var c:Number = matrix.c;
			var d:Number = matrix.d;
			
			matrix.a = a * cos - b * sin;
			matrix.b = a * sin + b * cos;
			matrix.c = c * cos - d * sin;
			matrix.d = c * sin + d * cos;
			
			quad.transform.matrix = matrix;
			
			txt.text = 'rotation\nmatrix.a = a * cos - b * sin;\nmatrix.b = a * sin + b * cos;\nmatrix.c = c * cos - d * sin;\nmatrix.d = c * sin + d * cos;\n';
		}
		
		private function scale( sx:Number, sy:Number):void 
		{
			
			matrix.a = sx;
			matrix.d = sy;
			
			circle.transform.matrix = matrix;
			txt.text = 'scale\nmatrix.a = sx;\nmatrix.d = sy;';
	
		}
		
		private function skew( sx:Number = 0, sy:Number = 0 ):void 
		{
			
			matrix.c = sx;
			matrix.b = sy;
			
			txt.text = 'skew\nmatrix.b = sy;\nmatrix.c = sx;';
			circle.transform.matrix = matrix;
			
		}
		
	}

}